<script>
    Namespace.register("EBCMS.Form_{$field.unique}");
    $(function() {

        /*改名名称*/
        EBCMS.Form_{$field.unique}.changename = function(id,value){
            if ($(id).is('div')) {
                /*编辑器*/
                $(id).next().attr('name','{$field.field}['+value+']');
            }else{
                $(id).attr('name','{$field.field}['+value+']');
            }
            $(id+'__config__').attr('name','{$field.field}[__config__]['+value+']');
        };
        EBCMS.Form_{$field.unique}.up = function(dom){
            var thisdom = $(dom).parent().parent().parent();
            if (thisdom.prev().length) {
                thisdom.insertBefore(thisdom.prev());
            }
        };
        EBCMS.Form_{$field.unique}.down = function(dom){
            var thisdom = $(dom).parent().parent().parent();
            if (thisdom.next().length) {
                thisdom.next().insertBefore(thisdom);
            }
        };
        EBCMS.Form_{$field.unique}.render = function(name,value,target){
            if (EBCMS.Form_{$field.unique}.config[name]) {
                
            }else{
                EBCMS.Form_{$field.unique}.config[name] = 'text';
            }
            if (EBCMS.Form_{$field.unique}['render_'+EBCMS.Form_{$field.unique}.config[name]]) {
                EBCMS.Form_{$field.unique}['render_'+EBCMS.Form_{$field.unique}.config[name]](name,value,target);
            }
        };
        EBCMS.Form_{$field.unique}.render_text = function(name,value,target){
            name = name||'EB_' + EBCMS.FN.random_str(6);
            opt = {
                name:name,
                value:value||'',
                id:'{$field.unique}_' + EBCMS.FN.random_str(15),
            };
            var str = '<tr>';
            str += '<td style="width:180px;">';
            str += '<div class="btn-group">';
            str += '<button type="button" class="btn btn-primary" onclick="$(this).parent().parent().parent().remove();">删除</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.up(this);">上移</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.down(this);">下移</button>';
            str += '</div>';
            str += '</td>';
            str += '<td style="width:120px;">';
            str += '<input type="text" class="form-control" value="'+opt.name+'" onKeyUp="EBCMS.Form_{$field.unique}.changename(\'#'+opt.id+'\',$(this).val());" placeholder="填写名称">';
            str += '</td>';
            str += '<td>';
            str += '<input type="text" class="form-control" id="'+opt.id+'" name="{$field.field}['+opt.name+']" value="'+opt.value+'" placeholder="填写内容">';
            str += '<input type="hidden" id="'+opt.id+'__config__" name="{$field.field}[__config__]['+opt.name+']" value="text">';
            str += '</td>';
            str += '</tr>';
            if (target) {
                $(target).append(str);
            }else{
                return str;
            }
        };
        EBCMS.Form_{$field.unique}.render_textarea = function(name,value,target){
            name = name||'EB_' + EBCMS.FN.random_str(6);
            opt = {
                name:name,
                value:value||'',
                id:'{$field.unique}_' + EBCMS.FN.random_str(15),
            };
            var str = '<tr>';
            str += '<td style="width:180px;">';
            str += '<div class="btn-group">';
            str += '<button type="button" class="btn btn-primary" onclick="$(this).parent().parent().parent().remove();">删除</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.up(this);">上移</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.down(this);">下移</button>';
            str += '</div>';
            str += '</td>';
            str += '<td style="width:120px;">';
            str += '<input type="text" class="form-control" value="'+opt.name+'" onKeyUp="EBCMS.Form_{$field.unique}.changename(\'#'+opt.id+'\',$(this).val());" placeholder="填写名称">';
            str += '</td>';
            str += '<td>';
            str += '<textarea class="form-control" id="'+opt.id+'" name="{$field.field}['+opt.name+']" rows="3" placeholder="填写内容">'+opt.value+'</textarea>';
            str += '<input type="hidden" id="'+opt.id+'__config__" name="{$field.field}[__config__]['+opt.name+']" value="textarea">';
            str += '</td>';
            str += '</tr>';
            if (target) {
                $(target).append(str);
            }else{
                return str;
            }
        };
        EBCMS.Form_{$field.unique}.render_file = function(name,value,target){
            name = name||'EB_' + EBCMS.FN.random_str(6);
            opt = {
                name:name,
                value:value||'',
                id:'{$field.unique}_' + EBCMS.FN.random_str(15),
            };

            var str = '<tr>';
            str += '<td style="width:180px;">';
            str += '<div class="btn-group">';
            str += '<button type="button" class="btn btn-primary" onclick="$(this).parent().parent().parent().remove();">删除</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.up(this);">上移</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.down(this);">下移</button>';
            str += '</div>';
            str += '</td>';
            str += '<td style="width:120px;">';
            str += '<input type="text" class="form-control" value="'+opt.name+'" onKeyUp="EBCMS.Form_{$field.unique}.changename(\'#'+opt.id+'\',$(this).val());" placeholder="填写名称">';
            str += '</td>';
            str += '<td>';
            str += '<table class="table" style="margin-bottom: 0px;">';
            str += '<tr>';
            str += '<td style="padding: 0px !important;">';
            str += '<input type="text" class="form-control" id="'+opt.id+'" name="{$field.field}['+opt.name+']" value="'+opt.value+'" placeholder="填写内容">';
            str += '<input type="hidden" id="'+opt.id+'__config__" name="{$field.field}[__config__]['+opt.name+']" value="file">';
            str += '</td>';
            str += '<td style="width: 100px;padding: 0px 0px 0px 10px !important;">';
            str += '<div id="'+opt.id+'_pick">上传</div>';
            str += '<script>';
            str += '$(function(){';
            str += '    EBCMS.MYFUN.renderUploader(\'#'+opt.id+'_pick\',function(file,res){ if (res.code) { $(\'#'+opt.id+'\').val(res.data.pathname); }else{ EBCMS.MSG.alert(res.msg);};});';
            str += '});';
            str += '<\/script>';
            str += '</td>';
            str += '</tr>';
            str += '</table>';
            str += '</td>';
            str += '</tr>';
            if (target) {
                $(target).append(str);
            }else{
                return str;
            }
        };
        EBCMS.Form_{$field.unique}.render_ueditor = function(name,value,target){
            name = name||'EB_' + EBCMS.FN.random_str(6);
            opt = {
                name:name,
                value:value||'',
                id:'{$field.unique}_' + EBCMS.FN.random_str(15),
            };

            var str = '<tr>';
            str += '<td style="width:180px;">';
            str += '<div class="btn-group">';
            str += '<button type="button" class="btn btn-primary" onclick="$(this).parent().parent().parent().remove();">删除</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.up(this);">上移</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.down(this);">下移</button>';
            str += '</div>';
            str += '</td>';
            str += '<td style="width:120px;">';
            str += '<input type="text" class="form-control" value="'+opt.name+'" onKeyUp="EBCMS.Form_{$field.unique}.changename(\'#'+opt.id+'\',$(this).val());" placeholder="填写名称">';
            str += '</td>';
            str += '<td>';
            str += '<textarea id="'+opt.id+'" name="{$field.field}['+opt.name+']" >'+opt.value+'</textarea>';
            str += '<script>';
            str += '$(function(){';
            str += '    EBCMS.MYFUN.renderUE("'+opt.id+'",{';
            str += '          autoHeightEnabled:false,';
            str += '          maximumWords:99999,';
            str += '          wordCount:true,';
            str += '          elementPathEnabled:true,';
            str += '          initialFrameHeight:400,';
            str += '    });';
            str += '});';
            str += '<\/script>';
            str += '<input type="hidden" id="'+opt.id+'__config__" name="{$field.field}[__config__]['+opt.name+']" value="ueditor">';
            str += '</td>';
            str += '</tr>';
            if (target) {
                $(target).append(str);
            }else{
                return str;
            }
        };
        
        var forms = {$field.value|json_encode};
        if (typeof forms!='object') {
            forms = {};
        }
        EBCMS.Form_{$field.unique}.config = forms['__config__']||{};
        delete forms['__config__'];
        $.each(forms, function(name, val) {
            EBCMS.Form_{$field.unique}.render(name,val,'#{$field.unique}_container');
        });
    });
</script>
<tr>
    <th>{$field.title}</th>
    <td>
        <div class="btn-group" role="group" aria-label="...">
            <button type="button" class="btn btn-primary btn-sm" onclick="EBCMS.Form_{$field.unique}.render_text('','','#{$field.unique}_container');">单行文本</button>
            <button type="button" class="btn btn-primary btn-sm" onclick="EBCMS.Form_{$field.unique}.render_textarea('','','#{$field.unique}_container');">多行文本</button>
            <button type="button" class="btn btn-primary btn-sm" onclick="EBCMS.Form_{$field.unique}.render_file('','','#{$field.unique}_container');">文件</button>
            <button type="button" class="btn btn-primary btn-sm" onclick="EBCMS.Form_{$field.unique}.render_ueditor('','','#{$field.unique}_container');">编辑框</button>
        </div>
        {notempty name='field.remark'}
            <p class="help-block">{$field.remark}</p>
        {/notempty}
    </td>
</tr>
<tr>
    <th></th>
    <td>
        <div class="table-responsive" style="border: 1px solid #ddd;">
            <table class="table table-noborder" id="{$field.unique}_container"></table>
            <input type="hidden" name="{$field.field}[__config__][__test__]" value='test'>
        </div>
    </td>
</tr>